/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.master.defaultservice;

import com.kmcsoft.emaster.entity.BusinessInformation;
import com.kmcsoft.emaster.entity.Employee;
import com.kmcsoft.emaster.entity.TrackableEntity;
import com.kmcsoft.security.SecurityUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.jdo.FetchPlan;
import javax.jdo.Query;

/**
 *
 * @author mylien
 */
public class DefaultGenericService extends com.kmcsoft.core.service.impl.DefaultGenericService {
//    public  Employee getCurentEmployee() {
//        List<Employee> employees = getJdoTemplate().execute(new JdoCallback<List<Employee>>() {
//            public List<Employee> doInJdo(PersistenceManager pm) throws JDOException {
//                Extent<Employee> extent = pm.getExtent(Employee.class, true);
//                Query query = pm.newQuery(extent);
//                query.setFilter("user.username.equals(\"" + SecurityUtil.getUsername() + "\")");
//                return (List<Employee>) query.execute();
//            }
//        });
//        if (employees != null) {
//            PersistenceManager pm = getPersistenceManager();
//            pm.getFetchPlan().addGroup("detach_emaster_Employee");
//            pm.getFetchPlan().setMaxFetchDepth(-1);
//            return pm.detachCopy(employees.iterator().next());
//        }
//        return null;
//    }
//
//    public void detachCurrentEmployee() {
//        FetchPlan fp = getPersistenceManager().getFetchPlan();
//        fp.addGroup("detach_emaster_Employee");
//        fp.setMaxFetchDepth(-1);
//    }

    /**     
     * @return Employee without detached.
     */
    public Employee getCurentEmployee() {
        Query query = getPersistenceManager().newQuery(Employee.class);
        query.setFilter("user.username.equals(\"" + SecurityUtil.getUsername() + "\")");
        query.setUnique(true);
        query.setRange("0,1");
        return getJdoTemplate().detachCopy((Employee) query.execute());
    }

    /**
     * This method return the collection of employees with the filter is username or name
     * @param usernameOrName the username or name of employee
     * @return Collection<Employee> without detached
     */
    public Collection<Employee> getEmployeeWithFilter(String usernameOrName) {
        Collection<Employee> employees = getJdoTemplate().find(Employee.class, "user.username.matches('" + usernameOrName + "') || name.matches('" + usernameOrName + "')");
        return getJdoTemplate().detachCopyAll(employees);
    }

    /**     
     * @return Collection<Employee> without detached
     */
    public Collection<Employee> getAllEmployees() {
        Collection<Employee> empsAfterFilter = new ArrayList<Employee>();
        if (null != getAllEmployeesWithAllTypes() && !getAllEmployeesWithAllTypes().isEmpty()) {
            for (Employee each : getAllEmployeesWithAllTypes()) {
                if (each.getType().equalsIgnoreCase(Employee.STAFF_TYPE) || each.getType().equalsIgnoreCase(Employee.PROBATIONARY_TYPE)) {
                    empsAfterFilter.add(each);
                }
            }
        }
        return empsAfterFilter;
    }

    public Collection<Employee> getAllEmployeesWithAllTypes() {
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(Employee.class));
    }

    @Override
    protected <T> void setTrackable(T pc) {
        if (pc instanceof TrackableEntity) {
            Employee currentEmpl = getCurentEmployee();
            TrackableEntity tc = (TrackableEntity) pc;

            if (tc.getId() > 0) {
                tc.setUpdatedBy(currentEmpl);
                tc.setUpdatedDate(new Date());
            } else {
                tc.setCreatedDate(new Date());
                tc.setCreatedBy(currentEmpl);
            }

            /*
             * if (JDOHelper.isPersistent(tc)) { tc.setUpdatedBy(currentEmpl);
             * tc.setUpdatedDate(new Date()); } else { tc.setCreatedDate(new
             * Date()); tc.setCreatedBy(currentEmpl); }
             */
        }
    }

    public BusinessInformation getBusinessInfo() {
        Collection<BusinessInformation> businessInformations = getJdoTemplate().find(BusinessInformation.class);
        final FetchPlan fp = getPersistenceManager().getFetchPlan();
        fp.addGroup("detach_BusinessInformation_All");
        fp.addGroup("detach_BankAccount_Bank");
        fp.addGroup("detach_Address_All");
        fp.setMaxFetchDepth(-1);
        if (businessInformations == null) {
            return null;
        }
        for (BusinessInformation businessInformation : businessInformations) {
            if (businessInformation.getParent() == businessInformation) {
                return getPersistenceManager().detachCopy(businessInformation);
            }
        }
        return null;
    }
}
